package com.yzx.lifeassistants.model.impl;

import java.util.Properties;

import android.content.Context;

import com.yzx.lifeassistants.model.IRenewBook;
import com.yzx.lifeassistants.model.analysis.RenewBookAnalysis;
import com.yzx.lifeassistants.model.callback.IRenewBookCB;
import com.yzx.lifeassistants.utils.HttpClientUtil;
import com.yzx.lifeassistants.utils.IHttpCallBack;
import com.yzx.lifeassistants.utils.LogcatUtils;
import com.yzx.lifeassistants.utils.PropertyUtil;

/**
 * @Description: 续借图书
 * @author: yzx
 * @time: 2016-3-10 下午8:52:51
 */
public class RenewBookImpl implements IRenewBook {
	public static final String TAG = "RenewBookImpl";
	private IRenewBookCB renewCallBack;
	private String url;

	public RenewBookImpl(Context context, IRenewBookCB renewCallBack) {
		this.renewCallBack = renewCallBack;
		Properties properties = PropertyUtil.getProperties(context,
				"base.properties");
		url = properties.getProperty("renew_my_book_url");
	}

	/**
	 * 
	 * @Description: 通过条形码续借图书
	 */
	@Override
	public void renewBook(String barcode) {
		HttpClientUtil.sendRequest(url + "?book_barcode=" + barcode, null,
				new IHttpCallBack() {

					@Override
					public void onFinish(String response) {
						LogcatUtils.i(response);
						boolean isSuccess = RenewBookAnalysis
								.isSuccess(response);
						if (isSuccess) {// 续借成功
							LogcatUtils.i("续借图书成功");
							renewCallBack.renewSuccess();
						} else {// 续借失败
							if (RenewBookAnalysis.alreadyRenew(response)) {
								LogcatUtils.e("因续借过一次而续借失败");
								renewCallBack
										.renewFailed(IRenewBookCB.ALREADY_RENEW);
							} else {
								LogcatUtils.e("因条形码出错而续借失败");
								renewCallBack
										.renewFailed(IRenewBookCB.RENEW_FAILED);
							}
						}
					}

					@Override
					public void onError(Exception e) {
						LogcatUtils.e("获取数据失败");
						if (null != e.getMessage()) {
							LogcatUtils.e(e.getMessage());
						}
						renewCallBack.renewFailed(IRenewBookCB.RENEW_FAILED);
					}
				});
	}

}
